<?php
class TW_Controller {

    public function __construct() {
        Session::initialize();
    }

    protected function loadView($name, $vars = null) {

        if(is_array($vars) && count($vars) > 0) {
            extract($vars, EXTR_PREFIX_SAME, 'data');
        }

        $file = VIEW_PATH . '/' . $name . '.phtml';

        if(!file_exists($file)) {
            $this->error("There's been an error: the view '{$name}' does not exist.");
        }

        require_once($file);

    }

    protected function loadModel($name, $alias = "") {
        $file = MODEL_PATH . '/' . $name . '.php';

        if(!file_exists($file)) {
            $this->error("There's been an error: the model '{$name}' does not exist.");
        }

        require_once($file);

        $class_name = ucfirst($name);

        if(class_exists($class_name)) {
            $this->$class_name = new $class_name();

            if($alias != '') {
                $this->$alias =& $this->$class_name;
            }

        }
        else {
            $this->error("Class '{$class_name}' was not found on '{$name}' model.");
        }

    }

    protected function error($msg) {
        throw new Exception($msg);
    }

}